<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>优车处置申请</title>
    <jsp:include page="../../import.jsp" flush="true"/>
    <jsp:include page="../assParking/assParkCommon.jsp" flush="true" /><!-- 时间格式转换 -->
    <script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
    <jsp:include page="../assCommFilesUpload.jsp" flush="true" />
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div>
        <div id="pageInTab" class="pageInTab">
            <!-- 页签1：优车处置申请 -->
            <div tabid="tabA" title="优车处置申请" lselected="true">
                <form id="yc_dealCar">
                    <div class="fieldset1 tabel_col clearFix">
                        <div class="fieldBox col_33">
                            <div class="label">申请人</div>
                            <div class="field">
                                <span id="applyNm" name="applyNm">${applyNm}</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33">
                            <div class="label">申请时间</div>
                            <div class="field">
                                <span id="applyDt" name="applyDt">${sysDate}</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33">
                            <div class="label">拖车标记</div>
                            <div class="field">
                                <span id="backedFlagCd" name="backedFlagCd">${backedFlagCd}</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33">
                            <div class="label">处置接收人类型</div>
                            <div class="field">
                                <select name="buyerTypeCd" id="buyerTypeCd" ligerui="width: 160" validate="{required:true}">
                                    <option value="" selected>请选择</option>
                                    <c:forEach items="${buyerTypeList }" var="buyer">
                                        <c:if test="${buyer.key == '1' || buyer.key == '2' }">
                                            <option value="${buyer.key }">${buyer.value }</option>
                                        </c:if>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="fieldBox col_33">
                            <div class="label">处置金额(元)</div>
                            <div class="field">
                                <input type="text" id="sellAmt" name="sellAmt" class="ui-textbox" ligerui="width: 160" value="">
                            </div>
                        </div>
                        <div class="fieldBox col_33">
                            <div class="label">处置路径</div>
                            <div class="field">
                                <select name="sellChannelCd" id="sellChannelCd" ligerui="width: 160">
                                    <option value="" selected>请选择</option>
                                    <c:forEach items="${sellChannelList }" var="sell">
                                        <option value="${sell.key }">${sell.value }</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <!-- 个人信息 -->
                        <div class="fieldBox col_33 person Hide">
                            <div class="label">处置接收人姓名</div>
                            <div class="field">
                                <input type="text" id="buyerNm" name="buyerNm" class="ui-textbox" ligerui="width: 160" value="" validate="{required:false}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33 person Hide">
                            <div class="label">处置接收人证件号</div>
                            <div class="field">
                                <input type="text" id="buyerCd" name="buyerCd" class="ui-textbox" ligerui="width: 160" value="" validate="{required:false}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33 person Hide">
                            <div class="label">联系方式</div>
                            <div class="field">
                                <input type="text" id="buyerTel" name="buyerTel" class="ui-textbox" ligerui="width: 160" value="" validate="{required:false}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <!-- 企业信息 -->
                        <div class="fieldBox col_33 company Hide">
                            <div class="label">处置接收企业名称</div>
                            <div class="field">
                                <input type="text" id="buyerCompanyNm" name="buyerCompanyNm" class="ui-textbox" ligerui="width: 160" value="" validate="{required:false}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33 company Hide">
                            <div class="label">处置接收企业联系人</div>
                            <div class="field">
                                <input type="text" id="buyerCompanyContact" name="buyerCompanyContact" class="ui-textbox" ligerui="width: 160" value="" validate="{required:false}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33 company Hide">
                            <div class="label">联系方式</div>
                            <div class="field">
                                <input type="text" id="buyerCompanyTel" name="buyerCompanyTel" class="ui-textbox" ligerui="width: 160" value="" validate="{required:false}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33 company Hide">
                            <div class="label">组织机构代码证</div>
                            <div class="field">
                                <input type="text" id="buyerCompanyCode" name="buyerCompanyCode" class="ui-textbox" ligerui="width: 160" value="" validate="{required:false}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_33 company Hide">
                            <div class="label"></div>
                            <div class="field"></div>
                        </div>
                        <div class="fieldBox col_33 company Hide">
                            <div class="label"></div>
                            <div class="field"></div>
                        </div>
                        <!-- 处置信息 -->
                        <div class="fieldBox col_100">
                            <div class="label">处置说明</div>
                            <div class="field">
                                <textarea name="sellDesc" id="sellDesc" rows="3" class="l-textarea" style="width:98%" value="" ></textarea>
                            </div>
                        </div>
                        <!-- 上传附件 -->
                        <div class="fieldBox col_33 Hide">
                            <div class="label">数据类型</div>
                            <div class="field">
                                <select name="dataTypeCd" id="dataTypeCd" ligerui="width: 160">
                                    <option value="">请选择</option>
                                    <c:forEach items="${dataTypeList }" var="data">
                                        <c:if test="${data.key == '2' }">
                                            <option value="${data.key }" selected>${data.value }</option>
                                        </c:if>
                                        <c:if test="${data.key != '2' }">
                                            <option value="${data.key }">${data.value }</option>
                                        </c:if>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <!-- 车辆交接单 -->
                        <div class="fieldBox col_33">
                            <div class="label">车辆交接单</div>
                            <div class="field">
                                <a class="detail" href="javascript:void(0);" onclick="entryOrder('detail')">查看</a>
                                <a href="javascript:void(0);" onclick="exportAssCarHvPdf()" style="margin-left: 30px;">导出</a>
                            </div>
                        </div>
                        <div class="fieldBox col_66">
                            <div class="label">拍卖状态</div>
                            <div class="field">
                                <span id="auctionCdNm" name="auctionCdNm">${auctionStsCdNm}</span>
                            </div>
                        </div>
                        <!-- 处置信息 -->
                        <div class="fieldBox col_100">
                            <div class="label">备注信息</div>
                            <div class="field">
                                <textarea name="sellDesc" id="sellDesc" rows="3" class="l-textarea" style="width:98%"
                                          value=""></textarea>
                            </div>
                        </div>
                        <div class="fieldBox col_33">
                            <div class="label">二次评估价(元)</div>
                            <div class="field">
                                <span id="baseAmt" name="baseAmt">${baseAmt}</span>
                            </div>
                        </div>
                        <div class="fieldBox col_66">
                            <div class="label"></div>
                            <div class="field"></div>
                        </div>
                        <!-- 上传附件 -->
                        <div class="fieldBox col_100">
                            <div class="label">附件上传</div>
                            <div class="fieldset1 tabel_col clearFix dealFileGrid">
                                <div class="sqPanel_body">
                                    <div id="dealFileGrid" class="sq-grid"></div>
                                </div>
                            </div>
                        </div>
                        <!-- 隐藏支付表主键、支付表号 -->
                        <div class="fieldBox col_100" style="display:none;">
                            <div class="field">
                                <input name="tcMasterId" id="tcMasterId" type="text" class="ui-textbox" value="${tcMasterId}">
                                <input name="tcDetailId" id="tcDetailId" type="text" class="ui-textbox" value="${tcDetailId}">
                                <input name="optimal_Id" id="optimal_Id" type="text" class="ui-textbox" value="${optimal_Id}">
                                <input name="prjPrdDtlId" id="prjPrdDtlId" type="text" class="ui-textbox" value="${prjPrdDtlId}">
                                <input name="wsPaySchId" id="wsPaySchId" type="text" class="ui-textbox" value="${wsPaySchId}">
                                <input name="wsPaySchNo" id="wsPaySchNo" type="text" class="ui-textbox" value="${wsPaySchNo}">
                                <input name="cstId" id="cstId" type="text" class="ui-textbox" value="${cstId}">
                                <input name="cstNm" id="cstNm" type="text" class="ui-textbox" value="${cstNm}">
                                <input name="orgName" id="orgName" type="text" class="ui-textbox" value="${orgName}">
                                <input name="buOrgCd" id="buOrgCd" type="text" class="ui-textbox" value="${buOrgCd}">
                                <!-- 以下参数为流程需要 -->
                                <input name="prjId" id="prjId" type="text" class="ui-textbox" value="${prjId}">
                                <input name="businessType" id="businessType" type="text" class="ui-textbox" value="${businessType}">
                                <input name="prjTypCd" id="prjTypCd" type="text" class="ui-textbox" value="${prjTypCd}">
                                <input name="prjNm" id="prjNm" type="text" class="ui-textbox" value="${prjNm}">
                                <input name="sueApplyId" id="sueApplyId" type="text" class="ui-textbox" value="${sueApplyId}">
                            </div>
                        </div>
                    </div>
                    <div class="btnsBox">
                        <span id="saveYCDealCarBtn" class="sq-button sq-button-round" onclick="saveYCDealCar()">发起申请</span>
                        <span id="closeBtn" class="sq-button sq-button-round" onclick="Remove_navTab()">关闭</span>
                    </div>
                </form>
            </div>
            <!-- 页签2：拖车单信息 -->
            <div tabid="tabB" title="拖车单信息" id="tabBId" style="">
                <iframe frameborder="0" name="tabB" id="tabB" src=""></iframe>
            </div>
        </div>
    </div>
</div>

<script>
    var pageInApp = {
        flag: false //tab页验证关闭
    };
    var pageInTab = null;//页签

    $(function () {
        initForm();//初始化表单
        initTab();//初始化页签
        initDealFileGrid();
        setDisplayAttr();//设置显示属性
    });

    var sellId = "${ycSellId}";

    var dealFileGrid = null;

    var fileGridRows;

    //设置显示属性
    function setDisplayAttr(){
        $("#buyerTypeCd").change(function(event) {
            var buyerTypeCd_val=$(this).val();
            var formSet1=liger.get("yc_dealCar");
            if (buyerTypeCd_val == "") {
                $("#yc_dealCar").find(".company").each(function (){
                    $(this).addClass('Hide');
                });
                $("#yc_dealCar").find(".person").each(function (){
                    $(this).addClass('Hide');
                });
            }else if (buyerTypeCd_val == "1") {//企业
                $("#yc_dealCar").find(".company").each(function (){
                    $(this).removeClass('Hide');
                });
                $("#yc_dealCar").find(".person").each(function (){
                    $(this).addClass('Hide');
                });
                //取消个人《必选》验证规则
                formSet1.setFieldValidate("buyerNm", {required: false});
                formSet1.setFieldValidate("buyerCd", {required: false});
                formSet1.setFieldValidate("buyerTel", {required: false});
                //设置企业《必选》验证规则
                formSet1.setFieldValidate("buyerCompanyNm", {required: true});
                formSet1.setFieldValidate("buyerCompanyContact", {required: true});
                formSet1.setFieldValidate("buyerCompanyTel", {required: true});
                formSet1.setFieldValidate("buyerCompanyCode", {required: true});
            }else if (buyerTypeCd_val == "2") {//个人
                $("#yc_dealCar").find(".person").each(function (){
                    $(this).removeClass('Hide');
                });
                $("#yc_dealCar").find(".company").each(function (){
                    $(this).addClass('Hide');
                });
                //取消企业《必选》验证规则
                formSet1.setFieldValidate("buyerCompanyNm", {required: false});
                formSet1.setFieldValidate("buyerCompanyContact", {required: false});
                formSet1.setFieldValidate("buyerCompanyTel", {required: false});
                formSet1.setFieldValidate("buyerCompanyCode", {required: false});
                //设置个人《必选》验证规则
                formSet1.setFieldValidate("buyerNm", {required: true});
                formSet1.setFieldValidate("buyerCd", {required: true});
                formSet1.setFieldValidate("buyerTel", {required: true});
            }
        });
    }

    // 维护过户方案资料Grid
    function initDealFileGrid() {
        dealFileGrid = $("#dealFileGrid").ligerGrid({
            url: '<%=path%>/ass/assDealSell/initDealFileGrid.do?id=' + sellId + '&cfgCd=ASS008',
            method: 'get',
            columns: [
                {display: '资料名称', name: 'datNm', align: 'center', minWidth: 100, enabledSort: false},
                {
                    display: '上传文件名称', name: 'fileNm', minWidth: 100, enabledSort: false,
                    render: function (rowdata, rowindex, value) {
                        if (value) {
                            var h;
                            h = '<a href="javascript:downloadFileByUrl(';
                            h += "'" + rowdata.filePath + "'";
                            h += ",'" + rowdata.fileNm + "'";
                            h += ')">' + rowdata.fileNm + '</a>'
                            return h;
                        }
                    }
                },
                {
                    display: '上传时间',
                    name: 'crtTm',
                    align: 'center',
                    width: 150,
                    enabledSort: false,
                    render: renderDate
                },
                {
                    display: '操作', name: 'operate', minWidth: 100, align: 'center', enabledSort: false,
                    render: function (rowdata, rowindex, value) {
                        var h = '';
                        h += '<a class="sq-button-hasicon uploadFile" style="cursor:pointer;" onclick="uploadFile(\'' + rowdata.cfgCd + '\', \'' + rowdata.cfgDtlCd + '\')"><i class="sqBtnIco_9"></i>上传</a><span class="sq-button sq-button-round fileupload-button" style="display: none;">上传<input type="file" id="uploadInput_' + rowdata.cfgCd + '_' + rowdata.cfgDtlCd + '" multiple name="fileup_' + rowdata.cfgCd + '_' + rowdata.cfgDtlCd + '"' +
                            ' class="fileupload-input" onchange="onChangefile_multi(event,\'' + rowdata.cfgCd + '\',\'' + rowdata.cfgDtlCd + '\',\'' + rowdata.orgCd + '\');"></span>';
                        if ('' != rowdata.fileId && null != rowdata.fileId) {
                            h += '<a class="sq-button-hasicon delFile" id="delFile_' + rowdata.cfgDtlCd + '" style="cursor:pointer;" onclick="delFile(event,' + rowdata.fileId + ');"><i class="sqBtnIco_7"></i>删除</a>';
                        }
                        return h;
                    }
                }
            ],
            root: 'rows',
            hideLoadButton: true,
            width: '100%',
            height: '200px',
            isScroll: false,
            rownumbers: true,  //显示序号
            checkbox: false,
            allowUnSelectRow: true,
            alternatingRow: false,
            frozen: false,
            usePager: false,
            onAfterShowData: function () {
                $('#dealFileGrid').rowspan('datNm');
                $('#dealFileGrid').rowspan('operate');
            },
            onSuccess: function (rowDatas, grid) {
                fileGridRows = rowDatas.rows.length;
            }
        });
    }

    //下载文件方法
    function downloadFileByUrl(_url,_name){
        if(SQ.regs.img.test(_name)){
            SQ.imgPreview(_url, true);
        }else{
            window.open(_url);
        }
    }

    // 上传文件a标签链接到input标签上
    function uploadFile(cfgCd, cfgDtlCd) {
        document.getElementById("uploadInput_" + cfgCd + "_" + cfgDtlCd).click();
    }

    /**
     * 上传文件
     * @param e
     * @param cfgCd
     * @param cfgDtlCd
     * @param orgCd
     */
    function onChangefile_multi(e, cfgCd, cfgDtlCd, orgCd) {
        var wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
        var filePath = "";
        filePath = "assDealFile";
        $.ajaxFileUpload({
            url: '<%=path%>/system/sysulfilesrec/upload.do',
            secureuri: false,
            fileElementId: e.target.id,  //这里不在是以前的id了，要写成数组的形式！
            dataType: 'json',
            data: {filePath: filePath},
            success: function (msg) {
                if (msg.success == 1) {
                    $.ajax({
                        type: 'post',
                        dataType: 'json',
                        data: {
                            'fileParams': JSON.stringify(msg.data),
                            'id': sellId,
                            'cfgCd': cfgCd,
                            'cfgDtlCd': cfgDtlCd,
                            'orgCd': orgCd
                        },
                        url: '<%=path%>/ass/assDealSell/uploadDealFile.do',
                        success: function (msg) {
                            if (msg.success == 1) {
                                dealFileGrid.reload();
                            } else {
                                $.ligerDialog.SQalert({content: "资料上传超时", SQ_type: "error"});
                            }
                            $.ligerDialog.hide();
                        }
                    });
                } else {
                    wait_dialog.close();
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    };

    // 格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
        }
        return dateStr;
    }

    //初始化表单
    function initForm() {
        $("#yc_dealCar").ligerForm({validate: true});
        $("#yc_dealCar").check_ligerFormEditor();//重新刷新页面动态字段
    }

    //初始化Tab页签
    function initTab() {
        var crflag = "detail";//查看交接单
        pageInTab = $("#pageInTab").ligerTab({
            height: 'auto',
            contextmenu: false,
            minIframeHeight: GET_minIframeHeight(),
            onAfterSelectTabItem: function (tabid) {
                switch (tabid) {
                    case "tabB":
                        var tcMasterId = $("#tcMasterId").val();
                        var tcDetailId = $("#tcDetailId").val();
                        if (!tcDetailId || !tcMasterId) {
                            $.ligerDialog.SQalert({content: "抱歉信息不存在！", SQ_type: "warning"});
                        } else {
                            pageInTab.setTabItemSrc(tabid, "<%=path%>/ass/assTrailCarM/toInfoViewPage.do?tcMasterId=" + tcMasterId + "&tcDetailId=" + tcDetailId + "&crflag=" + crflag);
                        }
                        break;
                }
                SetIframe_height();
            }
        });
        //设置页签
        $.extend(true, pageInApp, {
            pageInTab: pageInTab
        });
    };

    //保存优车处置申请信息（优车处置信息主表）
    function saveYCDealCar() {
        $.ligerDialog.confirm('确定发起流程吗?', function (flag) {
            if (flag == false) return;
            var ligerForm = liger.get("yc_dealCar");
            if (!ligerForm.valid()) {
                top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                return false; //验证不通过，不执行以下代码
            }
            //获取转优车表ID-为空提示
            var optimal_Id = $("#optimal_Id").val();
            if (!optimal_Id) {
                top.$.ligerDialog.SQalert({content: "转优车信息不存在,不能发起优车处置流程！", SQ_showButtons: true, SQ_type: "warning"});
                return false;
            }
            // for (var i = 0; i < fileGridRows; i++) {
            //     var row = dealFileGrid.getRow(i);
            //     if (null == row.fileId) {
            //         top.$.ligerDialog.SQalert({content: "请上传《购车合同》和《代偿结清协议》",SQ_type: "error"});
            //         return false;
            //     }
            // }
            //获取表单数据
            var data = ligerForm.getData();
            data.dataTypeCd = "2";
            data["ycSellId"] = sellId;
            //新增优车处置信息主表数据
            var wait_dialog = null;
            var url = '<%=path%>/ass/assTrailCarD/addYCDealInfo.do';
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: data,
                url: url,
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    //返回的优车处置信息主表ID不为空执行一下操作
                    if (msg.success == 1 && null != msg.data) {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_showButtons: true, SQ_type: "info"});
                    } else {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_showButtons: true, SQ_type: "error"});
                    }
                }
            });
        });
    }

    //出入库交接单
    function entryOrder(crflag) {
        var tcMasterId = $("#tcMasterId").val();//拖车主表ID
        var tcDetailId = $("#tcDetailId").val();//拖车明细ID
        var paySchId = $("#wsPaySchId").val();//支付表ID
        var paySchNo = $("#wsPaySchNo").val();//支付表号
        var prjId = $("#prjId").val();//项目ID
        var param = '&&tcMasterId=' + tcMasterId + '&&tcDetailId=' + tcDetailId + '&&crflag=' + crflag;
        param += '&&paySchId=' + paySchId + '&&paySchNo=' + paySchNo + '&&prjId=' + prjId;
        Add_navTab('entryOrder' + tcDetailId, '车辆交接单', '<%=path%>/ass/assTrailCarM/toWarehousePage.do?' + param);
    }

    //导出车辆交接单PDF
    function exportAssCarHvPdf() {
        var tcDetailId = $("#tcDetailId").val();//拖车明细ID
        window.location.href = '<%=path%>/ass/assTrailCarD/exportAssCarHvPdf.do?tcDetailId=' + tcDetailId;
    }
</script>
</body>
</html>